""">PAMR Plot Centroids

.. helpdoc::
Plot centroids made by fitting data to a PAMR model fit.  Once the pamr fit is made the data are shown as a tree view with each class as columns and the magnitude of the centroid for that class from the mean shown as lines.  The lines are plotted in decending order of magnitude of the shrunken centroid.

"""


"""<widgetXML>
    <name>PAMR Plot Centroids</name>
    <icon></icon>
    <tags>
        <tag>PAMR</tag>
    </tags>
    <summary>Plot PAMR Centroids</summary>
    <author>
        <authorname>Red-R Core Development Team</authorname>
        <authorcontact>www.red-r.org</authorcontact>
    </author>
    </widgetXML>
"""

from OWRpy import * 
import redRGUI, signals

class RedRpamr_plotcen(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.require_librarys(["pamr"])
        self.RFunctionParam_data = ''
        self.RFunctionParam_fit = ''
        
        """.. rrsignals::"""
        self.inputs.addInput("data", 'PAMR Data Collection', signals.pamr.RPAMRData, self.processdata)
        
        """.. rrsignals::"""
        self.inputs.addInput("fit", 'Trained PAMR Model Fit', signals.pamr.RPAMRFit, self.processfit)
        
        """.. rrgui::"""
        self.RFunctionParamthreshold_lineEdit = redRGUI.base.lineEdit(self.controlArea, label = "threshold:", text = '0')
        
        """.. rrgui::"""
        self.plotArea = redRGUI.plotting.redRPlot(self.controlArea, label = "Centroid Plot")
        
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    def processdata(self, data):
        
        if data:
            self.RFunctionParam_data=str(data.getData())
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_data=''
    def processfit(self, data):
        
        if data:
            self.RFunctionParam_fit=str(data.getData())
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_fit=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_data) == '': return
        if unicode(self.RFunctionParam_fit) == '': return
        if unicode(self.RFunctionParamthreshold_lineEdit.text()) == '': return
        injection = []
        if unicode(self.RFunctionParamthreshold_lineEdit.text()) != '':
            string = 'threshold='+unicode(self.RFunctionParamthreshold_lineEdit.text())+''
            injection.append(string)
        inj = ','.join(injection)
        self.plotArea.plot('data='+unicode(self.RFunctionParam_data)+',fit='+unicode(self.RFunctionParam_fit)+','+inj, function = 'pamr.plotcen')
    def getReportText(self, fileDir):
        if unicode(self.RFunctionParam_fit) == '': return 'No plot to show'
        elif unicode(self.RFunctionParam_fit) == '': return 'No plot to show'
        elif unicode(self.RFunctionParamthreshold_lineEdit.text()) == '': return 'No plot to show'
        else:
            self.R('png(file="'+fileDir+'/pamrPlotCV'+unicode(self.widgetID)+'.png")')
            injection = []
            if unicode(self.RFunctionParamthreshold_lineEdit.text()) != '':
                string = 'threshold='+unicode(self.RFunctionParamthreshold_lineEdit.text())+''
                injection.append(string)
            inj = ','.join(injection)
            self.R('pamr.plotcen(data='+unicode(self.RFunctionParam_data)+',fit='+unicode(self.RFunctionParam_fit)+','+inj+')')
            self.R('dev.off()')
            text = 'The following plot was generated using the threshold %s:\n\n' % (unicode(self.RFunctionParamthreshold_lineEdit.text()))
            text += '.. image:: '+fileDir+'/pamrPlotCV'+unicode(self.widgetID)+'.png\n    :scale: 50%%\n\n'
            return text
